﻿@{
    ViewBag.Title = "入库单";
    var st = ViewBag.ST as CubicFWNet.Data.ViewModal.StorageTableView;
    var orderList = ViewBag.OrderList as List<CubicFWNet.Data.ViewModal.OrderRelationView>;
}

<section class="content-header">
    <h1>
        入库单
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("AddPutInStorage", "StorageTable")">入库单</a></li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">
                <input type="hidden" name="ProductID" value="@st.ProductId" />
                <input type="hidden" name="ProData" id="ProData" />

                <div class="row">
                    <input type="hidden" id="SID" name="SID" value="@st.Id" />
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="ProductName" class="col-sm-3 control-label">产品名称</label>
                        <div class="col-sm-9">
                            <input type="text" onclick="ShowModal('@st.ProductId')" class="form-control linkLayout" id="ProductName" name="ProductName" value="@st.ProductName">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Format" class="col-sm-3 control-label">规格</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Format" name="Format" value="@st.Format" readonly>
                        </div>
                    </div>


                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Unit" class="col-sm-3 control-label">单位</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Unit" name="Unit" value="@st.Unit" readonly>
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Amount" class="col-sm-3 control-label">总数量</label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="Amount" name="Amount" value="@st.Amount" maxlength="20" readonly>
                        </div>
                    </div>
                </div>

                <div id="divProList" class="panelList">
                    @if (orderList != null)
                    {
                        foreach (var item in orderList)
                        {
                            <div class="row panelLayout" id="@item.OrderId">
                                <div class="form-group col-md-6 col-sm-12">
                                    <label class="col-sm-3 control-label">订单号</label>
                                    <div class="col-sm-9">
                                        <div class="form-control linkLayout">
                                            <span onclick="ShowOrderModal('@item.OrderId')">@item.Number</span>
                                            <span style="float:right" onclick='ShowInventory("@item.OrderId","@st.ProductId")'>查看库存</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-md-6 col-sm-12">
                                    <label class="col-sm-3 control-label userid" id="@item.UserId">订购人</label>
                                    <div class="col-sm-9 ">
                                        <span class="form-control txtLayout">@item.UserName</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6 col-sm-12">
                                    <label class="col-sm-3 control-label userid" id="@item.UserId">订购名称</label>
                                    <div class="col-sm-9 ">
                                        <span class="form-control txtLayout">@item.ProductName </span>                                
                                    </div>
                                </div>
                                <div class="form-group col-md-6 col-sm-12">
                                    <label class="col-sm-3 control-label">入库数量</label>
                                    <div class="col-sm-9 ">
                                        @if (orderList.Count == 1)
                                        {
                                            <input type="number" class="form-control amount" value="@st.Amount" readonly>
                                        }
                                        else
                                        {
                                            <input type="number" min="0" class="form-control amount" value="0">
                                        }
                                    </div>
                                </div>
                            </div>
                        }
                    }
                </div>
                <br />

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Memo" class="col-sm-3 control-label">备注</label>
                        <div class="col-sm-9">
                            <textarea rows="3" class="form-control" id="Memo" name="Memo" maxlength="500"></textarea>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Creator" class="col-sm-3 control-label">创建人</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Creator" name="Tolerance" value="@ViewBag.RealName" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CreateTime" class="col-sm-3 control-label">创建时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CreateTime" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm")" readonly="readonly" />
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-footer">
                <span class="btn btn-primary " onclick="FormSub()">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
                <a class="btn btn-primary" href="@Url.Action("PutInStorage","StorageTable")">返 回</a>
            </div>

        </div>
    </form>
</section>

@section Scripts {
    <script>


        //提交表单
        function FormSub() {

            //获取产品列表
            var proList = $("#divProList .panelLayout");
            var prodata = [];
            var num = proList.size();
            var total1 = 0;
            var total2=@st.Amount;
            var pid="@st.ProductId";
            for (i = 0; i < num; i++) {
                var oid = proList.eq(i).attr("id");
                var uid = $(".userid").eq(i).attr("id");
                var amount = $(".amount").eq(i).val();

                if (amount == "" || amount < 0) {
                    $.modalAlert("入库数量错误！", "warning");
                    return;
                }
                total1 += parseFloat(amount);
                prodata.push({ ProductID: pid,OrderID: oid, UserID: uid, Amount: amount });
            }

            total1=total1.toFixed(2);
            if(total1!=total2)
            {
                $.modalAlert("入库数量错误！", "warning");
                return;
            }

            $("#ProData").val(JSON.stringify(prodata));

            $("#formMain").submit();
        }

        //弹窗显示产品信息
        function ShowModal(id) {
            var options = {
                id: null,
                title: '产品信息',
                width: "90%",
                height: "90%",
                url: "../Product/Info?PID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }

        //弹窗显示订单信息
        function ShowOrderModal(id) {
            var options = {
                id: null,
                title: '订单信息',
                width: "90%",
                height: "90%",
                url: "../Order/OrderInfo?OID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }

        //查库存
        function ShowInventory(orderID, productID) {
            $.ajax({
                url: "/Order/ShowInventory",
                type: "Post",
                data: { oid: orderID, pid: productID },
                dataType: "json",
                async: false,
                success: function (data) {
                    $.modalAlert("库存数量：" + data, "success");
                }
            });
        }
    </script>
}